package com.group4.Servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.group4.DAO.UserDAO;
import com.group4.DAO.VideoCommentDAO;
import com.group4.DAO.VideoDAO;
import com.group4.DB.User;
import com.group4.DB.Video;
import com.group4.DB.VideoComment;

public class AddComment extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = -3329407929526153613L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json");
		// Get the printwriter object from response to write the required json
		// object to the output stream
		
		
		String videoId = request.getParameter("video_id");
		String text = request.getParameter("text");
		String userName = request.getParameter("username");

		PrintWriter out = response.getWriter();
		JsonObject jsonObject = new JsonObject();
		
		if(videoId==null || text == null || userName == null ){
			jsonObject.addProperty("error", "missing parameter!");
			out.print(jsonObject);
			out.flush();
		}else {
			UserDAO user = User.getUserWithUsername(userName);
			if( user != null ){
				VideoDAO video = Video.getVideoWithID(Integer.valueOf(videoId));
				if(video != null ){
					VideoCommentDAO comment = new VideoCommentDAO();
					comment.setCommentOwnerId(user.getID());
					comment.setCommentText(text);
					comment.setVideoId(Integer.valueOf(videoId));
					comment.setCommentVisible(true);
					java.util.Date utilDate = new java.util.Date();
					long time = utilDate.getTime();
					comment.setCommentDateTime(new Date(time));
					comment.setCommentRate(0);
					boolean res = VideoComment.addVideoComment(comment);
					if(res){
						Gson gson = new Gson();
			            String json = gson.toJson(comment);
			            JSONObject obj  = new JSONObject();
			            try {
							obj = (JSONObject)new JSONParser().parse(json);
							obj.put("username", user.getName());
						} catch (ParseException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
			            out.print(obj.toString());
						out.flush();
					} else {
						jsonObject.addProperty("error", "Error occured while adding comment!");
						out.print(jsonObject);
						out.flush();
					}
				 } else{
					jsonObject.addProperty("error", "No such video");
					out.print(jsonObject);
					out.flush();
				}
				
			}
			else{
				jsonObject.addProperty("error", "No such user");
				out.print(jsonObject);
				out.flush();
			}
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
				
	}
}
